﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace NTTDATA.BEENChina.ResultCertification.Util
{
    public class CompareString
    {
        /// <summary>
        /// 比较两个字符串，得到多录漏录的个数
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        public  int GetLeaveOutCount(string str1, string str2)
        {
            int leaveOutCount = 0;

            string maxString = str1.Length > str2.Length ? str1 : str2;
            string minString = str1.Length < str2.Length ? str1 : str2;

            for (int i = 0, j = 0; i < maxString.Length; i++)
            {
                if (j < minString.Length &&
                    maxString.Substring(i, 1) == minString.Substring(j, 1))
                {
                    j++;
                }
                else
                {
                    leaveOutCount++;
                }
            }
            return leaveOutCount;
        }

        /// <summary>
        /// 计算字符串相似度
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        public  decimal LevenshteinDistancePercent(string str1, string str2)
        {
            int val = Levenshtein_Distance(str1, str2);
            return (1 - (decimal)val / Math.Max(str1.Length, str2.Length)) * 100;
        }

        /// <summary>
        /// 取最小的一位数
        /// </summary>
        /// <param name="first"></param>
        /// <param name="second"></param>
        /// <param name="third"></param>
        /// <returns></returns>
        private  int LowerOfThree(int first, int second, int third)
        {
            int min = Math.Min(first, second);
            return Math.Min(min, third);
        }

        private  int Levenshtein_Distance(string str1, string str2)
        {
            int n = str1.Length;
            int m = str2.Length;

            int temp = 0;
            if (n == 0)
            {
                return m;
            }
            if (m == 0)
            {
                return n;
            }
            int[,] Matrix = new int[n + 1, m + 1];

            for (int i = 0; i <= n; i++)
            {
                //初始化第一列
                Matrix[i, 0] = i;
            }

            for (int j = 0; j <= m; j++)
            {
                //初始化第一行
                Matrix[0, j] = j;
            }

            for (int i = 1; i <= n; i++)
            {
                char ch1 = str1[i - 1];
                for (int j = 1; j <= m; j++)
                {
                    char ch2 = str2[j - 1];
                    temp = ch1.Equals(ch2) ? 0 : 1;
                    Matrix[i, j] = LowerOfThree(Matrix[i - 1, j] + 1, Matrix[i, j - 1] + 1, Matrix[i - 1, j - 1] + temp);
                }
            }
            return Matrix[n, m];
        }
    }
}
